package com.kexun.lds;



//  obj.wait(5000);   等待还可以设置时间  到时自动唤醒
public class Demo01 {
    public static void main(String[] args) {
        Object obj= new Object();

        new Thread(){
            @Override
            public void run() {

                while(true){
                    synchronized (obj){
                        System.out.println("告知老板买包子");
                        try {
                            obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                        //唤醒之后
                        System.out.println("吃包子");
                        obj.notify();


                    }
                }

            }
        }.start();


        new Thread(){
            @Override
            public void run() {

                //先睡 让上面的线程先执行
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }



                while(true){
                    synchronized (obj){

                        System.out.println("包子做好了");

                        obj.notify();

                        try {
                            obj.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        try {
                            Thread.sleep(2000);  //5秒做包子
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }

                    }
                }


            }
        }.start();
    }
}
